package com.thx.addressbook.dao;

import com.thx.addressbook.model.AddressbookCategory;
import com.thx.common.orm.PaginationDao;
import com.thx.common.util.ThreadLocalContext;
import com.thx.common.web.Page;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;



@Repository
public class AddressbookCategoryDao extends PaginationDao<AddressbookCategory> {

  private static final Logger logger = LoggerFactory.getLogger(AddressbookCategoryDao.class);

  /**
   * 根据通讯录分类id删除通讯录分类.
   * 
   * @param acId 不能为空
   */
  public void deleteCategoryById(Long acId) {
    this.delete(acId);
  }

  /**
   * 根据分类id查询通讯录分类对象.
   * 
   * @param acId 不能为空
   * @return 查询不到返回空对象
   */
  public AddressbookCategory findById(Long acId) {
    String hql = "from AddressbookCategory c where c.acId = ? ";
    List<AddressbookCategory> l = createQuery(hql, new Object[] {acId}).list();
    return l != null ? l.get(0) : null;
  }

  /**
   * 根据分类id、租户id查询通讯录分类对象.
   * 
   * @param acId 可以为空
   * @param enterpriseId 不能为空
   * @return 查询不到返回空对象
   */
  public AddressbookCategory findByIdAndEnterpriseId(Long acId, Long enterpriseId) {
    String hql = "from AddressbookCategory c where c.acId = ? and c.enterpriseId=? ";
    List<AddressbookCategory> l = createQuery(hql, new Object[] {acId, enterpriseId}).list();
    return l != null ? l.get(0) : null;
  }

  /**
   * 根据分类名称查询通讯录分类对象.
   * 
   * @param acName 不能为空
   * @return 查询不到返回空对象
   */
  public AddressbookCategory findByAcName(String acName) {
    String hql = "from AddressbookCategory c where c.acName = ? ";
    List<AddressbookCategory> l = createQuery(hql, new Object[] {acName}).list();
    return l != null ? l.get(0) : null;
  }

  /**
   * 根据租户分类id,租户id、分类名称组合条件分页查询通讯录分类对象列表.
   * 
   * @param authorizeIds 企业授权的分类id列表，可以为空
   * @param publicacIds 企业缺省的分类id列表，可以为空
   * @param acName 企业名称
   * @param enterpriseId 查询企业租户Id，不能为空
   * @return 企业租户id为空，返回空记录集
   */
  public List<AddressbookCategory> findByAcIdAndAcNameAndEnterpriseId(List<Long> authorizeIds,
      List<Long> publicacIds, String acName, Long enterpriseId) {
    List<AddressbookCategory> result = null;
    String hql = "from AddressbookCategory ";
    List<Object> objs = new ArrayList<Object>();
    if (enterpriseId != null && enterpriseId != 0) {
      Query query = this.generateEnterpriseConditionsQuery(authorizeIds, publicacIds, acName,
          enterpriseId, hql);
      if (hql.toUpperCase().indexOf("FROM") != -1) {
        Page<AddressbookCategory> page = (Page<AddressbookCategory>) ThreadLocalContext.get("page");
        int num = this.findAllCountByAcIdAndAcNameAndEnterpriseId(authorizeIds, publicacIds, acName,
            enterpriseId);
        if (num > 0) {
          page.setCount(num);
        } else {
          page.setCount(0);
        }
        result = query.setFirstResult(page.getStart()).setMaxResults(page.getMax()).list();
      }
    }
    return result;
  }



  /**
   * 根据,租户id、分类名称组合条件查询通讯录分类对象列表.
   * 
   * @param authorizeIds 企业授权的分类id列表，可以为空
   * @param publicacIds 企业缺省的分类id列表，可以为空
   * @param acName 企业名称
   * @param enterpriseId 查询企业租户Id，不能为空
   * @return 企业租户id为空，返回空记录集
   */
  public List<AddressbookCategory> findAllByAcIdAndAcNameAndEnterpriseId(List authorizeIds,
      List publicacIds, String acName, Long enterpriseId) {
    List<AddressbookCategory> result = null;
    String hql = "from AddressbookCategory ";
    List<Object> objs = new ArrayList<Object>();
    if (enterpriseId != null && enterpriseId != 0) {
      Query query = this.generateEnterpriseConditionsQuery(authorizeIds, publicacIds, acName,
          enterpriseId, hql);
      result = query.list();
    }
    return result;
  }

  /**
   * 根据租户分类id,租户id、分类名称组合条件查询通讯录分类对象列表数量.
   * 
   * @param authorizeIds 企业授权的分类id列表，可以为空
   * @param publicacIds 企业缺省的分类id列表，可以为空
   * @param acName 企业名称
   * @param enterpriseId 查询企业租户Id，不能为空
   * @return 企业租户id为空，返回空记录集
   */
  public int findAllCountByAcIdAndAcNameAndEnterpriseId(List<Long> authorizeIds,
      List<Long> publicacIds, String acName, Long enterpriseId) {
    int result = 0;
    String hql = " select count(*) from AddressbookCategory ";
    List list = new ArrayList();

    if (enterpriseId != null && enterpriseId != 0) {
      Query query = this.generateEnterpriseConditionsQuery(authorizeIds, publicacIds, acName,
          enterpriseId, hql);
      list = query.list();
    }
    if (list != null && list.size() > 0) {
      Long count = (Long) list.get(0);
      result = count.intValue();
    }
    return result;
  }


  private Query generateEnterpriseConditionsQuery(List<Long> authorizeIds, List<Long> publicacIds,
      String acName, Long enterpriseId, String hql) {
    List<Object> objs = new ArrayList<Object>();
    hql += " where enterpriseId = :enterpriseId";
    objs.add("enterpriseId");
    if (StringUtils.isNotBlank(acName)) {
      hql += " and acName like :acName ";
      objs.add("acName");
    }
    if (authorizeIds != null && authorizeIds.size() != 0) {
      hql += " and acId in (:authorizeIds) ";
      objs.add("authorizeIds");
    }
    if (publicacIds != null && publicacIds.size() != 0) {
      hql += " or acId in (:publicacIds) ";
      objs.add("publicacIds");
    }
    hql += " order by isPublic desc,createTime desc ";
    Query query = getSession().createQuery(hql);
    if (objs.contains("enterpriseId")) {
      query.setParameter("enterpriseId", enterpriseId);
    }
    if (objs.contains("acName")) {
      query.setParameter("acName", "%" + acName + "%");
    }
    if (objs.contains("authorizeIds")) {
      query.setParameterList("authorizeIds", authorizeIds);
    }
    if (objs.contains("publicacIds")) {
      query.setParameterList("publicacIds", publicacIds);
    }
    return query;
  }

  // private void generateEnterpriseConditions(List<Long> authorizeIds, List<Long> publicacIds,
  // String acName, String hql, List<Object> objs) {
  // hql += " where enterpriseId = :enterpriseId";
  // objs.add("enterpriseId");
  // if (StringUtils.isNotBlank(acName)) {
  // hql += " and acName like :acName ";
  // objs.add("acName");
  // }
  // if (authorizeIds != null && authorizeIds.size() != 0) {
  // hql += " and acId in (:authorizeIds) ";
  // objs.add("authorizeIds");
  // }
  // if (publicacIds != null && publicacIds.size() != 0) {
  // hql += " or acId in (:publicacIds) ";
  // objs.add("publicacIds");
  // }
  // hql += " order by isPublic desc,createTime desc ";
  // }
}
